package com.yang;


import android.Manifest;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.telephony.SmsMessage;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends PermissionActivity {

    private ListView listView;

    private List<Map<String, Object>> list = new ArrayList<>();

    private boolean isGranted = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = findViewById(R.id.listView);
        readSMS();
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, list, R.layout.list_item, new String[]{"tel", "type", "msg", "date"},
                new int[]{R.id.tv_phone, R.id.tv_type, R.id.tv_msg, R.id.tv_date}); // 自定义布局
        listView.setAdapter(simpleAdapter);
        simpleAdapter.notifyDataSetChanged();
    }

    private void readSMS() {
        requessPermission();
        Cursor cursor = null;
        if (isGranted) {
            ContentResolver resolver = getContentResolver();
            Uri uri = Uri.parse("content://sms/");
            cursor = resolver.query(uri, new String[]{"_id", "address", "type", "body", "date"}, null, null, null);
            if (cursor != null && cursor.getCount() > 0) {
                String smsType = "";
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                Date date;
                while (cursor.moveToNext()) {
                    Map map = new HashMap();
                    map.put("tel", cursor.getString(1));
                    smsType = cursor.getInt(2) == 1 ? "接收的短信" : "发送的短信";
//                    if (cursor.getInt(2) == 1) {
//                        smsType = "接收的短信";
//                    } else {
//                        smsType = "发送的短信";
//                    }
                    map.put("type", smsType);
                    map.put("msg", cursor.getString(3));
                    date = new Date(cursor.getLong(4));
                    map.put("date", simpleDateFormat.format(date));
                    list.add(map);
                }
            }
        }
        if (cursor != null) {
            cursor.close();
        }
    }

    // 请求授权方法
    private void requessPermission() {
        String[] permission = new String[]{Manifest.permission.READ_SMS}; //读取通讯录的授权
        requestRunTimePermission(permission, new PermissionListener() {
            @Override
            public void onGranted() {
                isGranted = true;
            }

            @Override
            public void onGranted(List<String> grantedPermission) {

            }

            @Override
            public void onDenied(List<String> deniedPermission) {
                isGranted = false;
            }
        });
    }
}
